package leetcode.L500;

public class EX297 {
    int point = 0;
    public String serialize(TreeNode root){
        return serialize(root,"");
    }

    public String serialize(TreeNode root, String res) {
        if(root == null) {
            res = res + "null,";
        }
        else {
            res = res + root.val + ',';
            res = serialize(root.left, res);
            res = serialize(root.right, res);
        }
        return res;

    }
    public TreeNode deserialize(String data) {
        String[] data_array = data.split(",");

        TreeNode res = new TreeNode();
        res = help(data_array);

        return res;
    }
    private TreeNode help(String[] l){
        if(l[point].equals("null")) {
            point ++;
            return null;
        }
        TreeNode root = new TreeNode(Integer.valueOf(l[point]));
        point ++;
        root.left = help(l);
        root.right = help(l);
        return root;
    }

}
